/****************************************************************************
  @file    : TCL_PJ.c
  @Version : V1.0
  @Company : Nanjing Puietel Internet of Things technology Co., Ltd.
  @author  : Lixiaolong
  @date    : 2019-02-20
  @brief   : TCL电视盒子
*****************************************************************************/
/* Includes ----------------------------------------------------------------*/
#include "tvbox.h"
#include "string.h"

/* Private typedef ---------------------------------------------------------*/
/* Private define ----------------------------------------------------------*/
//时间定义
#define  TCL_PJ_LEADCODE_TIME_H		(uint16_t)( 0x8000   | (uint16_t)9000 )
#define  TCL_PJ_LEADCODE_TIME_L		(uint16_t)( (~0x8000)& (uint16_t)4500 )
#define  TCL_PJ_ENDCODE_TIME_H		(uint16_t)( 0x8000   | (uint16_t)560  )
#define  TCL_PJ_ENDCODE_TIME_L	  (uint16_t)( (~0x8000)& (uint16_t)9945 )

#define  TCL_PJ_REPEAT0_TIME_H		(uint16_t)( 0x8000   | (uint16_t)9000  )
#define  TCL_PJ_REPEAT0_TIME_L	  (uint16_t)( (~0x8000)& (uint16_t)2250 )
#define  TCL_PJ_REPEAT1_TIME_H	  (uint16_t)( 0x8000	 | (uint16_t)600 )

#define  TCL_PJ_BIT0_TIME_H			(uint16_t)( 0x8000   | (uint16_t)560  )
#define  TCL_PJ_BIT0_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)560  )
#define  TCL_PJ_BIT1_TIME_H			(uint16_t)( 0x8000   | (uint16_t)560  )
#define  TCL_PJ_BIT1_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)1680 )

//键值编码
static const uint8_t TCL_PJCode[24][3] = {
	{ 0x0B,0x0A },	//0x0B,（开关）
	{ 0x0C,0x42 },	//0x0C,（菜单）
	{ 0x0D,0x1A },	//0x0D,（信息）
	{ 0x0E,0x40 },	//0x0E,（返回）
	{ 0x0F,0x0F },	//0x0F,（静音）
	{ 0x11,0x15 },	//0x11,（音量+）
	{ 0x12,0x1C },	//0x12,（音量-）
	{ 0x13,0x1F },	//0x13,（频道+）
	{ 0x14,0x1E },	//0x14,（频道-）
	{ 0x15,0x0B },	//0x15,（上）
	{ 0x16,0x0E },	//0x16,（下）
	{ 0x17,0x10 },	//0x17,（左）
	{ 0x18,0x11 },	//0x18,（右）
	{ 0x19,0x0D },	//0x19,（确定）
	{ 0x1E,0x45 },	//0x1E,（数字0）
	{ 0x1F,0x01 },	//0x1F,（数字1）
	{ 0x20,0x02 },	//0x20,（数字2）
	{ 0x21,0x03 },	//0x21,（数字3）
	{ 0x22,0x04 },	//0x22,（数字4）
	{ 0x23,0x05 },	//0x23,（数字5）
	{ 0x24,0x06 },	//0x24,（数字6）
	{ 0x25,0x07 },	//0x25,（数字7）
	{ 0x26,0x08 },	//0x26,（数字8）
	{ 0x27,0x09 },	//0x27,（数字9）
};

/* Private macro -----------------------------------------------------------*/
/* Private variables -------------------------------------------------------*/
static uint16_t TCL_PJSendlen = 0;

/* Private function prototypes ---------------------------------------------*/
static void TCL_PJPackageByte(uint8_t byte);

/* Private functions -------------------------------------------------------*/
static void TCL_PJPackageByte(uint8_t byte) {
	uint8_t i;

	for(i = 0; i<8; i++) {
		if(byte&(0x01<<(i))) {	//高电平
			IrSendBuf[TCL_PJSendlen++] = TCL_PJ_BIT1_TIME_H;
			IrSendBuf[TCL_PJSendlen++] = TCL_PJ_BIT1_TIME_L;
		} else {				//低电平
			IrSendBuf[TCL_PJSendlen++] = TCL_PJ_BIT0_TIME_H;
			IrSendBuf[TCL_PJSendlen++] = TCL_PJ_BIT0_TIME_L;
		}
	}
}

/* Public functions --------------------------------------------------------*/
void TCL_PJIrPackage(void *p) {
	uint8_t  i,p_byte[2] = {0};

	uint8_t codeNum = *(uint8_t*)p;;

	if((codeNum == 0)||(codeNum > TVBOX_KEY_NUM))	return ;

	for (i = 0; i < 24; i++) {
		if(TCL_PJCode[i][0] == codeNum ) {
			p_byte[0] = TCL_PJCode[i][1];
			p_byte[1] = 0xff-p_byte[0];//TCL_PJCode[i][2];
			break;
		}
	}

	if(p_byte[0] == 0) return;

	//while(IrDev.State != IRREMOTE_IDLE);  //等待发送完成

	memset(IrSendBuf, 0, sizeof(IrSendBuf)/sizeof(uint16_t));

	TCL_PJSendlen = 0;

	//配置引导码
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_LEADCODE_TIME_H;
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_LEADCODE_TIME_L;
	
	TCL_PJPackageByte(0x40);
	TCL_PJPackageByte(0x40);
	//配置控制码
	for(i = 0; i<2; i++)
		TCL_PJPackageByte(p_byte[i]);
	//配置结束码
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_ENDCODE_TIME_H;
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_ENDCODE_TIME_L;
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_ENDCODE_TIME_L;
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_ENDCODE_TIME_L;
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_ENDCODE_TIME_L;
	
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_REPEAT0_TIME_H;
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_REPEAT0_TIME_L;
	IrSendBuf[TCL_PJSendlen++] = TCL_PJ_REPEAT1_TIME_H;

	//组包完成提交
	IrDev.Ptr 		= 	IrSendBuf;
	IrDev.Len 		= 	TCL_PJSendlen;
	IrDev.State 	= IRREMOTE_REQUIRE;
}
